package com.tanhua.dubbo.api.db;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.api.mapper.QuestionMapper;
import com.tanhua.model.db.Question;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.management.Query;

/**
 * 问题服务接口实现类
 */
@DubboService
public class QuestionApiImpl implements QuestionApi{

    @Autowired
    private QuestionMapper questionMapper;

    /**
     * 根据当前用户id查询tb_question表 获取陌生人问题
     * @param userId
     * @return
     */
    @Override
    public Question findQuestionById(Long userId) {
        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);//user_name ==> userName
        return questionMapper.selectOne(queryWrapper);
    }

    /**
     * 设置陌生人问题 - 保存
     */
    @Override
    public void addOrUpdateQuestions(Question question) {
        //1根据userId查询问题记录是否存在
        Question qs = findQuestionById(question.getUserId());
        //2不存在 则保存
        if(qs == null){
            //qs = new Question();
            questionMapper.insert(question);
        }else {
            //3存在 则更新
            qs.setTxt(question.getTxt());//将消费者传入的问题设置到qs 对象
            questionMapper.updateById(qs);
        }
    }
}
